<?php

// +----------------------------------------------------------------------

// | OneThink [ WE CAN DO IT JUST THINK IT ]

// +----------------------------------------------------------------------

// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.

// +----------------------------------------------------------------------

// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>

// +----------------------------------------------------------------------



namespace Home\Model;

use Think\Model;

// use Home\Model\CourseRecord;



/**
 * 文档基础模型
 */

class MycourseModel extends Model{

	/**
	 * [showAll description]查找我想教的全部
	 * @param  [type] $id [description]用户登录的ID
	 * @return [type]     [description]
	 */

	public function showAll($id){

		$where['c.uid']        = $id;

		$where['c.status']     = 1;

		$where['m.is_del']     = 0;

		$result =

				D('Course')

				->alias('c')

				->join('tutor_mycourse as m on c.id = m.cid')

				->join('tutor_user_member as u on u.id = m.uid')

				->join('tutor_order as o on o.order=m.order')

				//->field('m.uid,m.finish,m.order,o.salary,c.title,c.id as cid,u.id as uid,u.nickname,u.age,u.logo,u.area_id,u.sex,u.students')
				->field('m.uid,m.finish,m.order,o.salary,m.teachname as title,c.id as cid,u.id as uid,u.nickname,u.age,u.logo,u.area_id,u.sex,u.students')

				->where($where)

				->order('m.id desc')

				->select();

		foreach ($result as &$value) {

				$value['class_date'] = $this ->showTime($value['order']);

				$value['passed']   = $this ->attrPassed($value['uid']);

				$value['area_id2'] = getAreaId($value['area_id']);

				//去掉整数后的.00

				$value['salary'] = ($value['salary'] == (int)$value['salary']) ? (int)$value['salary'] : $value['salary'];

		}

		return $result;

	}

	/**

	 * [showStudent description]查找相当学生的全部信息

	 * @param  [type] $id [description]用户的ID

	 * @return [type]     [description]

	 */

	public function showStudent($id){

		$where['m.uid'] 	 = $id;

		$where['c.status']   = 1;

		$where['m.is_del']   = 0;

		$result = $this

			 ->alias('m')

			 ->join('tutor_course as c on m.cid = c.id')

			 ->join('tutor_user_member as u on u.id = c.uid')

			 ->join('tutor_order as o on m.order = o.order')

			 ->field('c.id,c.uid,o.salary,c.title,m.order,m.uid as course_id,m.finish,u.nickname,m.uid as user_id,u.age,u.sex,u.area_id,u.logo,u.students')

			 ->where($where)

			 ->order('m.id desc')

			 ->select();

			foreach ($result as &$value) {

				$value['class_date'] = $this ->showTime($value['order']);

				$value['passed']   = $this ->attrPassed($value['uid']);

				$value['area_id2'] = getAreaId($value['area_id']);

				//去掉整数后的.00

				$value['salary'] = ($value['salary'] == (int)$value['salary']) ? (int)$value['salary'] : $value['salary'];

				//查找是否实名认证

				// $res = D('Auth') -> find($value['uid']);

				// if (!empty($res)) {

				// 	$value['passed'] = $res['passed'];

				// }

			}

		return $result;

	}

	/**

	 * [showTime description]查找下一节课上课的时间

	 * @param  [type] $cid [description]课程ID

	 * @param  [type] $uid [description]用户ID

	 * @return [type]      [description]

	 */

	public function showTime($order){

		$amp['o.order'] = $order;

		$amp['r.status'] = 1;

		$amp['r.class_date'] = array('gt',time());

		$data = D('CourseRecord')

						 ->alias('r')

						 ->join('tutor_order as o on o.id = r.order_id')

						 ->field('r.class_date')

						 ->where($amp)

						 ->order('r.class_date')

						 ->find();

		return $data['class_date'];

	}

	/**

	 *查找教学生的老师的资料的方法；

	 */

	public function selectOne($uid,$cid,$order){

		$map['order'] = $order;

		$map['m.cid'] = $cid;

		$map['m.uid'] = $uid;

		$map['c.status'] = 1;

		$result = $this

				  ->alias('m')

				  ->join('tutor_course as c on c.id = m.cid')

				  ->join('tutor_user_member as u on u.id = c.uid')

				  ->join('tutor_auth as a on u.id = a.uid')

				  ->field('m.uid as user_id,m.cid as m_cid,m.cancel,m.finish,c.uid as cid,c.id,c.title,c.passed,c.salary,u.nickname,u.age,u.sex,u.logo,u.area_id,m.uid,u.students,a.passed')

				  ->where($map)

				  ->find();
				  // echo M() ->_Sql();die;
				  // dump($result);die;
		return $result;

	}

	/**
	 * [showTimeNum description]获取小于当前时间的课时
	 * @param  [type] $time [description]所有时间
	 * @param  [type] $date [description]当前时间
	 * @return [type]       [description]
	 */

	public function showTimeNum($time,$date){

		$num = 0;

		foreach ($time as $value) {

			if($value['class_date'] > $date){

				$num +=1;

			}

		}

		return $num;

	}

	/**

	 * [showTimeGt description]获取大于当前时间的课时

	 * @param  [type] $time [description]所有时间

	 * @param  [type] $date [description]当前时间

	 * @return [type]       [description]

	 */

	public function showTimeGt($time,$date){

		$num = 0;

		foreach ($time as $value) {

			if($value['class_date'] > $date){

				$num +=1;

			}

		}

		return $num;

	}

	/**

	 * [compare description]查询是否上完课程

	 * @param  [type] $time [description] 所有上课的时间

	 * @return [type]       [description]

	 */

	public function compare($time){

		$date = time();//实例化当前时间戳做对比

		foreach ($time as $value) {

			$data[] = $value['class_date'];

		}

		foreach($data as $vo){

			    if($vo<$date){

			    	$num = 1;

			    }else{

			    	$num = 2;

			    }

			}

		return $num;

	}

	/**

	 * Ajax修改上课时间

	 * @param  [type] $data [description]

	 * @return [type]       [description]

	 */

	public function mystuAjax($data){

		$id = $data['id'];

		$class_date = strtotime($data['date']);

		$date = time();

		if($class_date < $date){

			return "修改不能小于当前时间";

		}

		$data = D('CourseRecord') ->where(array('id' =>$id))->save(array('class_date' =>$class_date));

		if($data){

			return "课程更改成功";

		}else{

			return "课程更改失败";

		}

	}

	/**

	 * 查询一条时间

	 */

	public function showTimeOne($uid,$cid){

		$map['uid'] 	 = $uid;

		$map['cid'] 	 = $cid;

		$map['at_class'] = 0;

		$map['status']   = 1;

		//dump($map);die;

		$data = D('CourseRecord') ->where($map)->find();

		return $data;

	}

	/**

	 * 学生确认方法

	 * @param  [type] $ids [description]

	 * @return [type]      [description]

	 */

	public function mystuAjaxStu($ids){

		D('CourseRecord') ->where(array('id' =>array('in',$ids)))->save(array('at_class' =>1));

	}

	/**

	 *判断是否大于一节课

	 * @param  [type] $ids [description]

	 */

	public function geArr($ids){

		//循环查找KEY

		foreach ($ids as $key => $value) {

			if($key > 0){

				//返回1做判断用

				return 1;

			}

		}

	}

	/**
	 * 修改钱
	 * @param  [type] $rid 上完的课程的ID
	 */

	public function updateSalary($rid,$order){
		$where['id'] = $rid;

		//先查出自己的ID和班级ID

		$ruid = D('CourseRecord') ->field('cid,uid') ->where($where)->find();
// dump($ruid);
		$map['o.order'] = $order;

		//根据班级ID查找出教师ID

		$cuid = D('CourseRecord') ->alias('r') ->join('tutor_order as o on r.order_id = o.id') ->field('o.tid as uid,o.salary') ->where($map) ->find();
// dump($cuid);
		//实例化需要事物的模型

		$m=D('UserMember');

		// $m->startTrans();//开启事物

		//老师一节课的钱

		$csalary = floor($cuid['salary'] * 93)/100;
// dump($csalary);
		//查询系统信息

		$where['id'] = 0;

		//上一个行锁，安全第一。

		$root    = $m ->lock(true) ->where($where) ->find();

		//平台减去

		$salary  = $root['account'] - $csalary;
		// dump($root['account']);
		// dump($salary);
		$result  = $m ->where($where)->save(array('account' =>$salary));
// 		echo M() ->_Sql();die;
// dump($result);die;



		//添加平台余额流水

		$data['uid'] = 0;

		$data['change'] = $csalary;

		$data['action'] = 1;

		$data['obj'] = $cuid['uid'];

		$data['create_time'] = time();

		$res1 = D('Account') -> addAccount($data);



		//查找教师的信息

		$s['id'] = $cuid['uid'];

		$data    = $m ->where($s) ->find();

		//执行sql操作

		$account = floatval($data['account']) + $csalary;

		$result2 = $m->where($s)->save(array('account' =>$account));



		//添加教师余额流水

		$tea['uid'] = $cuid['uid'];

		$tea['change'] = $csalary;

		$tea['action'] = 2;

		$tea['obj'] = 0;

		$tea['create_time'] = time();

		$res2 = D('Account') -> addAccount($tea);



		// dump($result2);die;

		if($result && $result2 && $res1 && $res2){

			$m->commit();//成功则提交

			//给所有人发消息

			$salary = $cuid['salary'];

			$teach  = $cuid['salary'] * 0.93;

			$econ   = $cuid['salary'] * 0.07;

			D('Message') ->MsgAll($ruid['uid'],$cuid['uid'],$csalary,$econ,$salary);

			return true;

		}else{
			// dump($result);
			// dump($result2);
			// dump($$res1);
			// dump($res2);die;
			$m->rollback();//不成功，则回滚

			return false;

		}

	}



	/**

	 * 检查用户是否有未完成的课程

	 * @param  int  $uid [用户id]

	 * @return array     [用户课程条目]

	 * @author qlj

	 */

	// public function is_complete($uid=''){

	// 	$where['uid'] = empty($uid) ? session('uid') : $uid;

	// 	$where['type'] = 0;

	// 	//未结束

	// 	$where['finish'] = 0;

	// 	//未退订

	// 	$where['cancel'] = 0;

	// 	return $this -> where($where) -> select();

	// }

	/**

	 * 第一次点击增加学生个数

	 * @return [type] [description]

	 */

	public function changeStudents($id){

		//取出uid

		$res  = D('CourseRecord') ->field('cid,uid') ->where(array('id' => $id)) ->find();

		$uid  = $res['uid'];

		//查找看有没有点击过

		$map['uid'] = $uid;

		$map['at_class'] = array('gt','0');

		$data = D('CourseRecord') ->where($map) ->select();

		if(empty($data)){

			//查找出班级里的UID老师id

			$user_id = D('Course') ->field('uid') ->where(array('id' =>$res['cid'])) ->find();

			//把赋值的变量更新

			//教师的ID

			$update = $user_id['uid'];

			$result = D('UserMember') -> field('students') -> where(array('id'=>$update)) ->find();

			//加一个学生

			$where['students'] = $result['students'] + 1;

			D('UserMember') ->where(array('id' =>$update)) ->data($where) ->save();

		}

	}

	/*

	 *查找以上完三天内的课程

	 *

	 */

	public function isCourse($order){

		// $map['m.uid'] = $uid;

		// $map['m.cid'] = $cid;

		$map['m.order'] = $order;

		$map['r.at_class'] = 0;

		$map['r.status']   = 1;

		$map['r.class_date'] = array('lt',time());

		$result = $this

					->alias('m')

					->join('tutor_order as o on m.order = o.order')

					->join('tutor_course_record as r on o.id = r.order_id')

					->field('r.class_date')

					->where($map)

					->order('r.class_date desc')

					->find();
			//lkl修改3天倒计时
			$time = $result['class_date'] + 60*60*24*3;

			$t = $time - time();

			return $t;

	}

	/**

	 * 获取退订的字段信息

	 */

	public function isCancel($uid,$cid){

		$map['uid'] = $uid;

		$map['cid'] = $cid;

		$map['cancel'] = 1;

		return $this ->where($map)->count();

	}

	/**

	 *替换是否已经认证

	 */

	public function attrPassed($id){

		$data = D('Auth') ->field('passed') ->where(array('id' =>$id)) ->find();

		return $data['passed'];

	}

	/*

	 *退订的时候改为退订中...

	 */

	public function updateCancel($uid,$cid,$order){

		$result = D('Order') ->where(array('order' =>$order)) ->find();

		$where['order_id'] = $result['id'];

		$where['class_date'] = array('gt',time());

		$where['at_class'] = 0;

		D('CourseRecord') ->where($where) ->save(array('class_date' =>0));

		$map['uid']   = $uid;

		$map['cid']   = $cid;

		$map['order'] = $order;

		$time = time();

		$data  = $this ->where($map) ->save(array('cancel' =>2,'cancel_time' =>$time));

		if($data){

			D('Message') ->daituidin($uid,$cid);

		    D('Sms') ->daituidin($uid,$cid);

			return true;

		}else{

			return false;

		}

	}

	/**

	 *

	 */

	public function autoCancelTime(){

		$map['cancel_time'] = array('lt',time());

		$map['cancel'] = 2;

 		$data = $this ->where($map) ->select();

		foreach ($data as &$value) {

			if(time() > ($value['cancel_time'] + (3600 * 24 ) * 2 ) ){

				D('CourseRecord') ->ajaxCancel($value['order']);

			}

		}

		return $data;

	}

}